package com.test08_面向对象应用;

import org.junit.Test;

public class YootkDemo_04 {

    /**
     * 字符串统计：面向过程
     */
    @Test
    public void test1() {
        String str = "want you to know one thing";
        String[] result = str.split("");
        int oCount = 0;
        int nCount = 0;
        for (String temp : result) {
            if ("o".equalsIgnoreCase(temp)) {
                oCount++;
            } else if("n".equalsIgnoreCase(temp)) {
                nCount++;
            }
        }
        System.out.println("字母O的个数：" + oCount + "、字母N的个数：" + nCount);
    }

    /**
     * 字符串统计：面向对象
     */
    @Test
    public void test2() {
        String str = "want you to know one thing";
        StringUtil util = new CharCount(str);
        System.out.println(util);
    }
}

/**
 * 字符串工具类
 */
class StringUtil {
    private String content;

    public StringUtil(String content) {
        this.content = content;
    }

    public String getContent() {
        return this.content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

/**
 * 实现数据统计
 */
class CharCount extends StringUtil {
    private int oCount;
    private int nCount;

    public CharCount(String content) {
        super(content);  // 调用父类中的单参构造
        this.handleContent();   // 构造方法直接进行字符串的处理
    }

    /** 处理传入的数据信息 */
    public void handleContent() {
        String[] result = super.getContent().split("");
        for (String temp : result) {
            if ("o".equalsIgnoreCase(temp)) {
                this.oCount++;
            } else if("n".equalsIgnoreCase(temp)) {
                this.nCount++;
            }
        }
    }

    @Override
    public String toString() {
        return "【字符统计结果】字母“O“的个数" + this.oCount + "、字母“N”的个数：" + this.nCount;
    }
}
